﻿@model Rail.Net.Core.Model.Nonpersistent.OffersModel
@using Rail.Net.Controllers
@using Rail.Net.Common.Resources

@{
    ViewBag.Title = "eRailnet - " + Global.Main_BreadCrumb_Offers;
}

<script type="text/javascript">
    jQuery.fn.dataTableExt.oSort['numeric-comma-asc'] = function (a, b) {
    var x = (a == "-") ? 0 : a.replace(/,/, ".");
    var y = (b == "-") ? 0 : b.replace(/,/, ".");
    x = parseFloat(x);
    y = parseFloat(y);
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};

jQuery.fn.dataTableExt.oSort['numeric-comma-desc'] = function (a, b) {
    var x = (a == "-") ? 0 : a.replace(/,/, ".");
    var y = (b == "-") ? 0 : b.replace(/,/, ".");
    x = parseFloat(x);
    y = parseFloat(y);
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};

function ClientDataBegin() {
    $('#client-data-content').html("");
    $('#payment-content').html("");
    $('#ticket-content').html("");
    $.blockUI({
        css:
        {
            width: '700px',
            height: '500px',
            top:  ($(window).height() - 500) /2 + 'px', 
            left: ($(window).width() - 700) /2 + 'px'
        },
        message: $('#loading')
    });
}
function ClientDataSuccess() {
    $.unblockUI();
    $('client-data').append('<div style="clear:both"></div>');

    $('a[href="#client-data"]').triggerHandler('click');
    $('a[href="#client-data"]').triggerHandler('change');
    
    //$('#tabs').tabs('select', '#client-data');
}

function ClientDataFailure() {
    $.unblockUI();
    alert("Error occured!");
}

function validateProducts() {
    var valid = true;
    var outwardProductId = $('#outwardProductId').val();
    if (outwardProductId == '') {
        valid = false;
        $('#outward_message').show();
    }
    else {
        $('#outward_message').hide();
    }
    var returnProductId = $('#returnProductId').val();
    if (returnProductId == '') {
        valid = false;
        $('#return_message').show();
    }
    else {
        $('#return_message').hide();
    }

    return valid;
}

var oTable1;
var oTable2;

$(document).ready(function () {

    $('#dialog_content').dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
            }
        },
        close: function () { }
    });

    oTable1 = $('#offersTable1').dataTable({
        "sDom": 'T<"clear">lfrtip',
        "oTableTools": {
            "sRowSelect": "single",
            "sSwfPath": "/Content/images/TableTools/copy_cvs_xls_pdf.swf",
            "aButtons": [
				{
				    "sExtends": "print",
				    "sButtonText": "@Global.Main_Offers_Print",
				    "sInfo": "@Global.Main_Offers_PrintFinished",
				    "mColumns": [0, 1, 2, 3, 4, 5, 6, 7, 9]
				},
                {
				    "sExtends": "xls",
				    "sButtonText": "Excel",
				    "sNewLine": "auto",
				    "sFieldBoundary": '"',
				    "sFieldSeperator": "\t",
				    "sCharSet": "utf16le",
				    "mColumns": [0, 1, 2, 3, 4, 5, 6, 7, 9]
				},
                {
                    "sExtends": "pdf",
                    "sButtonText": "PDF",
                    "sPdfSize": "A3",
                    "mColumns": [0, 1, 2, 3, 4, 5, 6, 7, 9]
                }
			],
            "fnRowSelected": function (node) {
                var id = node.id.replace('product_outward_', '');
                $('#outwardProductId').val(id);
            },
            "fnRowDeselected": function () {
                $('#outwardProductId').val('');
            }
        },
        "bJQueryUI": true,
        "bSortClasses": false,
        "sPaginationType": "full_numbers",
        "bStateSave": true,
        "aoColumns": [null, null, null, null, null, null, null, null, null, { "sType": "numeric-comma" }, null],
        "oLanguage": {
            "sProcessing": "@Global.DataTable_sProcessing",
            "sLengthMenu": "@Global.DataTable_sLengthMenu",
            "sZeroRecords": "@Global.DataTable_sZeroRecords",
            "sInfo": "@Global.DataTable_sInfo",
            "sInfoEmpty": "@Global.DataTable_sInfoEmpty",
            "sInfoFiltered": "@Global.DataTable_sInfoFiltered",
            "sInfoPostFix": "@Global.DataTable_sInfoPostFix",
            "sSearch": "@Global.DataTable_sSearch",
            "sUrl": "@Global.DataTable_sUrl",
            "oPaginate": {
                "sFirst": "@Global.DataTable_oPaginate_sFirst",
                "sPrevious": "@Global.DataTable_oPaginate_sPrevious",
                "sNext": "@Global.DataTable_oPaginate_sNext",
                "sLast": "@Global.DataTable_oPaginate_sLast"
            }
        }
    });

    oTable2 = $('#offersTable2').dataTable({
        "sDom": 'T<"clear">lfrtip',
        "oTableTools": {
            "sRowSelect": "single",
            "sSwfPath": "/Content/images/TableTools/copy_cvs_xls_pdf.swf",
            "aButtons": [
				{
				    "sExtends": "print",
				    "sButtonText": "@Global.Main_Offers_Print",
				    "sInfo": "@Global.Main_Offers_PrintFinished",
				    "mColumns": [0, 1, 2, 3, 4, 5, 6, 7, 9]
				},
                {
                    "sExtends": "xls",
                    "sButtonText": "Excel",
                    "sNewLine": "auto",
                    "sFieldBoundary": '"',
                    "sFieldSeperator": "\t",
                    "sCharSet": "utf16le",
                    "mColumns": [0, 1, 2, 3, 4, 5, 6, 7, 9]
                },
                {
                    "sExtends": "pdf",
                    "sButtonText": "PDF",
                    "sPdfSize": "A3",
                    "mColumns": [0, 1, 2, 3, 4, 5, 6, 7, 9]
                }
			],
            "fnRowSelected": function (node) {
                var id = node.id.replace('product_return_', '');
                $('#returnProductId').val(id);
            },
            "fnRowDeselected": function () {
                $('#returnProductId').val('');
            }
        },
        "bJQueryUI": true,
        "bSortClasses": false,
        "sPaginationType": "full_numbers",
        "bStateSave": true,
        "aoColumns": [null, null, null, null, null, null, null, null, null, { "sType": "numeric-comma" }, null],
        "oLanguage": {
            "sProcessing": "@Global.DataTable_sProcessing",
            "sLengthMenu": "@Global.DataTable_sLengthMenu",
            "sZeroRecords": "@Global.DataTable_sZeroRecords",
            "sInfo": "@Global.DataTable_sInfo",
            "sInfoEmpty": "@Global.DataTable_sInfoEmpty",
            "sInfoFiltered": "@Global.DataTable_sInfoFiltered",
            "sInfoPostFix": "@Global.DataTable_sInfoPostFix",
            "sSearch": "@Global.DataTable_sSearch",
            "sUrl": "@Global.DataTable_sUrl",
            "oPaginate": {
                "sFirst": "@Global.DataTable_oPaginate_sFirst",
                "sPrevious": "@Global.DataTable_oPaginate_sPrevious",
                "sNext": "@Global.DataTable_oPaginate_sNext",
                "sLast": "@Global.DataTable_oPaginate_sLast"
            }
        }
    });

    $('#offers-back-btn').click(function (e) {
        e.preventDefault();
        //$('#tabs').tabs('select', 0);
        $('a[href="#selection"]').triggerHandler('click');
        $('a[href="#selection"]').triggerHandler('change');
        
        return false;
    });

    $('#offers-next-btn').click(function (event) {
        if (!validateProducts()) {
            event.preventDefault();
            $("#dialog_content").dialog("open");
            return false;
        }
        return true;
    });

    $('div .dataTables_wrapper div').each(function (i) {
        $(this).removeClass("ui-widget-header");
    });

    $('table tbody tr').each(function (i) {
        $(this).removeClass("ui-state-highlight");
    });

});

</script>

@(Html.MakePopup<DetailsController>(o => o.Details(0, true), modal: true, width: 900, height: 700, resizable: false))


@{ Html.EnableClientValidation(); }
@using (Ajax.BeginForm("ClientData", "Ticket", new { }, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "client-data-content", InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace, OnBegin = "ClientDataBegin", OnSuccess = "ClientDataSuccess", OnFailure = "ClientDataFailure" }, new { id = "OffersForm" }))
{
    <fieldset>
        <legend>@Global.Main_BreadCrumb_Offers</legend>
        <h2 align="center">
            @Model.SearchModel.FromStationName - @Model.SearchModel.ToStationName, @Model.SearchModel.FromDate.ToShortDateString()
        </h2>
        @Html.Hidden("outwardProductId", "", new { id = "outwardProductId" })
        @if (Model.ForwardDirectionProducts != null && Model.ForwardDirectionProducts.Products.Count > 0)
         {
             <table id="offersTable1" class="dataTable" style="width: 100%;">
                 <thead>
                     <tr>
                         <th>
                             @Global.Main_Offers_FromStation
                         </th>                               
                         <th>
                             @Global.Main_Offers_ToStation
                         </th>
                         <th>
                             @Global.Main_Offers_DepartureTime
                         </th>
                         <th>
                             @Global.Main_Offers_ArrivalTime
                         </th>
                         <th>
                             @Global.Main_Offers_Duration
                         </th>
                         <th>
                             @Global.Main_Offers_Transfers
                         </th>
                         <th>
                             @Global.Main_Offers_Class
                         </th>
                         <th>
                             @Global.Main_Offers_FreeSeats
                         </th>
                         <th>
                         </th>
                         <th>
                             @Global.Main_Offers_Price €
                         </th>
                         <th>
                         </th>
                     </tr>
                 </thead>
                 <tbody>
            
                     @{
                         foreach (var product in Model.ForwardDirectionProducts.Products.Where(p => p.Connection.DepartureTime >= Model.SearchModel.FromDate).OrderBy(p => p.Connection.DepartureTime))
                         {
                             <tr id="product_outward_@(product.Index)">
                                 <td>
                                     @product.Connection.FirstStation.Name
                                 </td>                            
                                 <td>
                                     @product.Connection.LastStation.Name
                                 </td>
                                 <td class="right">
                                     @product.Connection.DepartureTime.ToShortTimeString()
                                 </td>
                                 <td class="right">
                                     @product.Connection.ArrivalTime.ToShortTimeString()
                                 </td>
                                 <td class="right">
                                     @product.Connection.Duration.ToString("hh':'mm")
                                 </td>  
                                 <td class="right">
                                     @product.Connection.Transfers
                                 </td>
                                 <td>
                                    <img src="@(Url.Content("~/Content/images/class_" + product.ServiceLevelGroup.ToLower() + ".png"))" style="float: left; padding-right: 10px; width: 16px;" alt="ComfortClass" />
                                    <a class="tooltip" href="#">
                                        @product.ComfortClass. (@Global.ResourceManager.GetString("Main.Offers.ClassGroup" + product.ServiceLevelGroup))
                                        <span class="custom info">
                                            <img src="../../Content/images/info.png" alt="Info" height="48" width="48" class="png" />
                                            <em>Info</em>@Global.ResourceManager.GetString("Main.Offers.Class" + product.ServiceLevel)
                                        </span>
                                    </a>
                                 </td>
                                 <td class="right">
                                     @if (product.NumberOfFreeSeats != int.MaxValue)
                                     {
                                         @product.NumberOfFreeSeats
                                     }
                                     else
                                     {
                                         @Global.Main_Offers_NoLimit
                                     }
                                 </td>
                                 <td class="right">
                                     @if (product.IsContengentProduct)
                                     {
                                         <img src="@Url.Content("~/Content/images/shamrock_16x16.png")" alt="Clover" />
                                     }
                                     else if (product.IsGlobalProduct)
                                     {
                                         <img src="@Url.Content("~/Content/images/globe_16x16.png")" alt="Globe" />
                                     }
                                 </td>
                                 <td class="right">
                                     @product.Price.ToString("0.00")
                                 </td>                              
                                 <td>
                                     <div class="center">
                                         @(Html.PopupActionLink<DetailsController>(a => a.Details(product.Index, true), htmlAttributes: new { @class = "abtn ui-state-default ui-corner-all" }))
                                     </div>
                                 </td>
                             </tr>
                         }

                     }
            
                 </tbody>
             </table>
         }
            
        <div style="clear:both;"></div>
    
        @if (Model.ReturnDirectionProducts != null && Model.ReturnDirectionProducts.Products.Count > 0)
        {
            <h2 align="center">
                @Model.SearchModel.ToStationName - @Model.SearchModel.FromStationName, @Model.SearchModel.ToDate.Value.ToShortDateString()
            </h2>
            @Html.Hidden("returnProductId", "", new { id = "returnProductId" })
            <table id="offersTable2" class="dataTable" style="width: 100%;">
                <thead>
                    <tr>
                        <th>
                            @Global.Main_Offers_FromStation
                        </th>                               
                        <th>
                            @Global.Main_Offers_ToStation
                        </th>
                        <th>
                            @Global.Main_Offers_DepartureTime
                        </th>
                        <th>
                            @Global.Main_Offers_ArrivalTime
                        </th>
                        <th>
                            @Global.Main_Offers_Duration
                        </th>
                        <th>
                            @Global.Main_Offers_Transfers
                        </th>
                        <th>
                            @Global.Main_Offers_Class
                        </th>
                        <th>
                            @Global.Main_Offers_FreeSeats
                        </th>
                        <th>
                        </th>
                        <th>
                            @Global.Main_Offers_Price €
                        </th>
                        <th>
                        </th>
                    </tr>
                </thead>
                <tbody>
                
                    @{
            foreach (var product in Model.ReturnDirectionProducts.Products.Where(p => p.Connection.DepartureTime >= Model.SearchModel.ToDate.Value).OrderBy(p => p.Connection.DepartureTime))
            {
                            <tr id="product_return_@(product.Index)">
                                <td>                                
                                    @product.Connection.FirstStation.Name
                                </td>                                
                                <td>
                                    @product.Connection.LastStation.Name
                                </td>
                                <td class="right">
                                    @product.Connection.DepartureTime.ToShortTimeString()
                                </td>
                                <td class="right">
                                    @product.Connection.ArrivalTime.ToShortTimeString()
                                </td>
                                <td class="right">
                                    @product.Connection.Duration.ToString("hh':'mm")
                                </td>  
                                <td class="right">
                                    @product.Connection.Transfers
                                </td>
                                <td>
                                    <img src="@(Url.Content("~/Content/images/class_" + product.ServiceLevelGroup.ToLower() + ".png"))" style="float: left; padding-right: 10px; width: 16px;" alt="ComfortClass" />
                                    <a class="tooltip" href="#">
                                        @product.ComfortClass. (@Global.ResourceManager.GetString("Main.Offers.ClassGroup" + product.ServiceLevelGroup))
                                        <span class="custom info">
                                            <img src="../../Content/images/info.png" alt="Info" height="48" width="48" class="png" />
                                            <em>Info</em>@Global.ResourceManager.GetString("Main.Offers.Class" + product.ServiceLevel)
                                        </span>
                                    </a>
                                </td>
                                 <td class="right">
                                    @if (product.NumberOfFreeSeats != int.MaxValue)
                                    {
                                        @product.NumberOfFreeSeats
                                    }
                                    else
                                    {
                                        @Global.Main_Offers_NoLimit
                                    }
                                </td>
                                <td class="right">
                                    @if (product.IsContengentProduct)
                                    {
                                        <img src="@Url.Content("~/Content/images/shamrock_16x16.png")" alt="Clover" />
                                    }
                                    else if (product.IsGlobalProduct)
                                    {
                                        <img src="@Url.Content("~/Content/images/globe_16x16.png")" alt="Globe" />
                                    }
                                </td>
                                <td class="right">
                                    @product.Price.ToString("0.00")
                                </td>
                                <td>
                                    <div class="center">
                                        @(Html.PopupActionLink<DetailsController>(a => a.Details(product.Index, false), htmlAttributes: new { @class = "abtn ui-state-default ui-corner-all" }))
                                    </div>
                                </td>                            
                            </tr>
            }} 
                
                </tbody>
            </table>
            <div style="clear:both;"></div>
        }        
    </fieldset>
    <div class="center">
        <input id="offers-back-btn" type="submit" name="OffersBackBtn" value="@Global.Main_Offers_Back" />
        <input id="offers-next-btn" type="submit" name="OffersNextBtn" value="@Global.Main_Offers_Next" />
    </div>
}

<div id="dialog_content">
    <div class="ui-state-error">
        <p>
            <span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>
            @Global.Main_Offers_PleaseSelect
        </p>
        <ul>
            <li id="outward_message">@Global.Main_Offers_OneOutwardProduct</li>
            <li id="return_message">@Global.Main_Offers_OneReturnProduct</li>
        </ul>
    </div>
</div>